package com.itheima.udp3;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ServerDemo {

    public static void main(String[] args) throws Exception {
        //创建 接收端对象  必须指定端口号
        DatagramSocket ds = new DatagramSocket(8888);
        //创建接受数据的对象
        byte[] bytes = new byte[1024*64];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        //接受   阻塞
        while (true) {
            ds.receive(dp);
            //组织数据
            //ds
            //byte[] data = dp.getData();
            int length = dp.getLength();
            InetAddress address = dp.getAddress();
            String s = new String(bytes,0, length);
            System.out.println(address.getHostAddress()+address.getHostName()+":"+s);
        }
        //释放资源
        //ds.close();

    }
}
